#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
Simplified BSD License (BSD)
Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 17-Nov-09 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;

using Thn;
#endregion

namespace Thn.Interface.Vcl
{
    /// <summary>
    /// A general purpose multi-page control that render page header as tabs.
    /// </summary>
    [ToolboxItem(true)
    , DefaultProperty("Pages")
    , DefaultEvent("AfterTabChanged")
    , Designer(typeof(TabControlDesigner))
    , ToolboxBitmap(typeof(ResourceBeacon), "Thn.Interface.Vcl.Resources.Icons.TabControl.bmp")]
#if DEBUG
    public
#else
    public
#endif
 class TabControl : TabControlBase
    {
        #region Fields
        #endregion

        #region Properties

        #region Pages

        /// <summary>
        /// Gets/Sets list of pages.
        /// </summary>
        [Browsable(true), Category("Data"), Description("List of pages")
        , DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public ControlContainerList Pages
        {
            get { return mPages; }
            set
            {
                //unassociate previous value
                if (mPages != null) mPages.Host = null;
                #region Defensive tracing
                else
                {
                    //no previous
                }
                #endregion

                mPages = value;
                mPages.Host = this;
                Invalidate();
            }
        }
        #endregion

        #region Selected Index
        /// <summary>
        /// Gets/Sets index of the current page for displaying.
        /// </summary>
        [Browsable(true), DefaultValue(-1), Category("Data"), Description("Index of the current page for display")]
        public int SelectedIndex
        {
            get { return mSelectedIndex; }
            set
            {
                //normalize value
                if (mPages.Count < 1) value = -1;
                else if (value >= mPages.Count) value = mPages.Count - 1;
                #region Defensive tracing
                else
                {
                    //value is valid
                }
                #endregion

                TabChangeEventArgs args = new TabChangeEventArgs();
                args.SelectedIndex = value;

                //raise before change
                RaiseBeforeTabChanged(args);

                //apply the change if not canceled
                if (!args.Canceled)
                {
                    #region Apply the change

                    mSelectedIndex = value;
                    RenderContext.SelectedIndex = value;
                    Invalidate();
                    ShowPage(value);

                    //raise event
                    RaiseAfterTabChanged(args);

                    #endregion
                }
                else
                {
                    #region Canceled

                    //the change was rejected

                    #endregion
                }
            }
        }
        #endregion

        #region Tab Width
        /// <summary>
        /// Gets/Sets horizontal size of a tab.
        /// </summary>
        [Browsable(true), DefaultValue(75), Category("Appearance"), Description("Horizontal size of a tab.")]
        public int TabWidth
        {
            get { return mTabWidth; }
            set
            {
                mTabWidth = value;
                InvalidateLayout();
            }
        }
        #endregion

        #region Tab Height
        /// <summary>
        /// Gets/Sets vertical size of a tab.
        /// </summary>
        [Browsable(true), DefaultValue(30), Category("Appearance"), Description("Vertical size of a tab.")]
        public int TabHeight
        {
            get { return mTabHeight; }
            set
            {
                mTabHeight = value;
                InvalidateLayout();
            }
        }
        #endregion

        #region Tab Size
        /// <summary>
        /// Gets/Sets size of tab titles region.
        /// </summary>
        [Browsable(true), DefaultValue(35), Category("Appearance"), Description("Size of tab titles region.")]
        public int TabSize
        {
            get { return mTabRegionSize; }
            set
            {
                mTabRegionSize = value;
                InvalidateLayout();
            }
        }
        #endregion

        #endregion

        #region Events

        #region Before Tab Changed
        /// <summary>
        /// This event is raised before a tab is selected.
        /// </summary>
        public event TabChangeHandler BeforeTabChanged;

        /// <summary>
        /// Invoke event BeforeTabChanged
        /// </summary>
        [Browsable(true), Category("Action"), Description("Occurs before a tab is selected.")]
        protected void RaiseBeforeTabChanged(TabChangeEventArgs args)
        {
            TabChangeHandler handler = BeforeTabChanged;
            if (handler != null) handler(this, args);
        }
        #endregion

        #region After Tab Changed
        /// <summary>
        /// This event is raised after a tab is selected.
        /// </summary>
        public event TabChangeHandler AfterTabChanged;

        /// <summary>
        /// Invoke event AfterTabChanged
        /// </summary>
        [Browsable(true), Category("Action"), Description("Occurs after a tab is selected.")]
        protected void RaiseAfterTabChanged(TabChangeEventArgs args)
        {
            TabChangeHandler handler = AfterTabChanged;
            if (handler != null) handler(this, args);
        }
        #endregion

        #endregion

        #region On Tab Selected By Mouse
        /// <summary>
        /// This method is raised when user clicks on a specific tab.
        /// </summary>
        protected override void OnTabSelectedByMouse(int tabIndex)
        {
            SelectedIndex = tabIndex;
        }
        #endregion

        #region Internals

        #region Get Render Context
        /// <summary>
        /// Gets the current render context.
        /// </summary>
        internal TabControlRenderContext GetRenderContext()
        {
            return this.RenderContext;
        }
        #endregion

        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor.
        /// </summary>
        public TabControl()
        {
            this.Pages = new ControlContainerList();
            mCacheBackground = false;
        }
        #endregion
    }

    /// <summary>
    /// Custom delegate for events used by <see cref="TabControl"/>.
    /// </summary>
    /// <param name="sender">The object that raised this event.</param>
    /// <param name="e">Arguments used for the event.</param>
#if DEBUG
    public
#else
    public
#endif
 delegate void TabChangeHandler(object sender, TabChangeEventArgs e);
}
